package seyeralfredo.com.db;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * Created by Alfredo on 02-Aug-14.
 */
public class DBManager {
    private static DBManager instance = new DBManager();
    private SessionFactory sessionFactory = null;

    private DBManager() {
        if (sessionFactory == null) {
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
    }

    public static DBManager getInstance() {
        return instance;
    }

    public Session getSession() {
        return sessionFactory.openSession();
    }


}
